#include "table.h"
#include "util.h"

// #define TEST_BOOK_T
#define TEST_USER_T
// #define TEST_BORROW_T

int main()
{

#ifdef TEST_USER_T
    table_t *user_tbl = createTable(USER);
    record_t record;
    record.table = USER;
    int count = 30;

    while (count--) {
        int id = count;
        generateRecord(id, &record, USER);
        if (insert(user_tbl, &record) == 1)
            break;
    }
    printTable(user_tbl);
    Travel(user_tbl->tree);
    TravelData(user_tbl->tree);
#define WHAT
#ifdef WHAT
    count = 20;
    while (count--) {
        int id = count;
        printf("delete key: %d\n", id);
        delete(user_tbl, id);
    }

    printTable(user_tbl);
    Travel(user_tbl->tree);
    TravelData(user_tbl->tree);

    count = 20;
    while (count--) {
        int id = count;
        printf("insert key: %d\n", id);
        generateRecord(id, &record, USER);
        if (insert(user_tbl, &record) == 1)
            break;
    }

    count = 10;
    while (count--) {
        int id = rand() % 100 + 30;
        generateRecord(id, &record, USER);
        if (modify(user_tbl, count,  &record) == 1)
            break;
    }

    printTable(user_tbl);
    Travel(user_tbl->tree);
    TravelData(user_tbl->tree);

    count = 20;
    int find_count = 0;
    while (count--) {
        int id = rand() % 100;
        if (search(user_tbl, id,  &record) == 0) {
            printf("find it: %d\n", id);
            find_count += 1;
            printOneRecord(&record, record.table);
        }
    }
    printf("total find key: %d\n", find_count);
#endif /* WHAT */

    destroyTable(user_tbl);

#endif  /* TEST_USER_T */

#ifdef TEST_BOOK_T
    table_t *book_tbl = createTable(BOOK);
    record_t record;
    int count = 100;

    while (count--) {
        int id = count;
        generateRecord(id, &record, BOOK);
        if (insert(book_tbl, &record) == 1)
            break;
    }

    printTable(book_tbl);
    Travel(book_tbl->tree);
    TravelData(book_tbl->tree);

    count = 20;
    while (count--) {
        int id = count;
        printf("delete key: %d\n", id);
        delete(book_tbl, id);
    }

    printTable(book_tbl);
    Travel(book_tbl->tree);
    TravelData(book_tbl->tree);

    count = 20;
    while (count--) {
        int id = count;
        printf("insert key: %d\n", id);
        generateRecord(id, &record, BOOK);
        if (insert(book_tbl, &record) == 1)
            break;
    }

    printTable(book_tbl);
    Travel(book_tbl->tree);
    TravelData(book_tbl->tree);

    destroyTable(book_tbl);

#endif /* TEST_BOOK_T */

#ifdef TEST_BORROW_T
    table_t *borrow_tbl = createTable(BORROW);
    record_t record;
    int count = 100;

    while (count--) {
        int id = count;
        generateRecord(id, &record, BOOK);
        if (insert(borrow_tbl, &record) == 1)
            break;
    }

    printTable(borrow_tbl);
    Travel(borrow_tbl->tree);
    TravelData(borrow_tbl->tree);

    count = 20;
    while (count--) {
        int id = count;
        printf("delete key: %d\n", id);
        delete(borrow_tbl, id);
    }

    printTable(borrow_tbl);
    Travel(borrow_tbl->tree);
    TravelData(borrow_tbl->tree);

    count = 20;
    while (count--) {
        int id = count;
        printf("insert key: %d\n", id);
        generateRecord(id, &record, BORROW);
        if (insert(borrow_tbl, &record) == 1)
            break;
    }

    printTable(borrow_tbl);
    Travel(borrow_tbl->tree);
    TravelData(borrow_tbl->tree);

    destroyTable(borrow_tbl);
#endif /* TEST_BORROW_T */

    return 0;
}
